// pages/person/cut.js
const app = getApp()
var wh = require("../../utils/wh.js");
Page({

  /**
   * 页面的初始数据
   */
  data: { 
    istrue: true,
    list: [],
    lists: [],
    page: 1,
    page_size: 5,
    no_data: 0,
    idxTime: "",//当前时间戳
    onearth: false, // 是否已经到达底部
    idxClick: 0,
    down: "",
    downs: "",
    have: true,
    haves: true
  },
  nav() {
    clearInterval(this.data.down)
    clearInterval(this.data.downs)
    let istrue = this.data.istrue
    this.setData({
      istrue: !istrue,
      page: 1,
      page_size: 5,
      no_data: 0,
      onearth: false,
      list: [],
      lists: [],
      cutdaowbefore: false,
      have: true,
      haves: true
    })
    if (this.data.istrue) {
      this.ColletcGoods()
    } else {
      this.CollectActivity()
    }
  },
  // 	我的秒杀列表（正在进行）
  ColletcGoods() {
    let that = this;
    let page_size = this.data.page_size,
      no_data = this.data.no_data,
      page = this.data.page;
    if (no_data == 0) {
      wh.Post('/api/mall.seckill/mylist', {
        page: page,
        page_num: page_size,
      }, function (res) {
        let idxtime = Date.parse(new Date()) / 1000,//当前时间
          restTime;//剩余时间
        console.log('正在进行', res)
        let shop = that.data.list.concat(res),
          count = res.length;
        for (let i = 0; i < shop.length;i++){
          restTime = (shop[i].end_time - idxtime);
          let idxList = shop[i],
            restTime = { "restTime": restTime };
          shop[i] = Object.assign(idxList, restTime)
        }
        console.log(res)
        if (page_size > count) {
          that.data.no_data = 1;
          if (shop.length > 3) {
            that.setData({
              onearth: true
            })
          }
        }
        that.setData({
          list: shop
        })
        if (that.data.list.length == 0) {
          that.setData({
            have: false,
            haves: true,
          });
        } else {
          that.setData({
            have: true,
            haves: true,
          });
        }
      })
    }
    setTimeout(function () { that.countdown(); }, 0)
    var down = setInterval(function () { that.countdown(); }, 1000)
    this.setData({ down: down })
  },

  // 	我的秒杀预约列表（尚未开始）
  CollectActivity() {
    let that = this;
    let page_size = this.data.page_size,
      no_data = this.data.no_data,
      page = this.data.page;
    if (no_data == 0) {
      wh.Post('/api/mall.seckill/my_notice', {
        page: page,
        page_num: page_size,
      }, function (res) {
        let idxtime = Date.parse(new Date()) / 1000,//当前时间
          restTime;//剩余时间
        console.log('尚未开始', res)
        let shop = that.data.lists.concat(res),
          count = res.length;
        for (let i = 0; i < shop.length; i++) {
          restTime = (shop[i].start_time - idxtime);
          let idxList = shop[i],
            restTime = { "restTime": restTime };
          shop[i] = Object.assign(idxList, restTime)
        }
        console.log(res)
        if (page_size > count) {
          that.data.no_data = 1;
          if (shop.length > 3) {
            that.setData({
              onearth: true
            })
          }
        }
        that.setData({
          lists: shop
        })
        if (that.data.lists.length == 0) {
          that.setData({
            haves: false,
            have: true,
          });
        } else {
          that.setData({
            haves: true,
            have: true,
          });
        }
      })
    }
    setTimeout(function () { that.countdowns(); }, 0)
    var downs = setInterval(function () { that.countdowns(); }, 1000)
    this.setData({ downs: downs })
  },
  //删除(正在进行)
  collect_del: function (e) {
    let that = this,
      lists = this.data.lists,
      list = this.data.list,
      sponsor_id = e.currentTarget.dataset.id,
      inx = e.currentTarget.dataset.inx;
    wx.showModal({
      title: '',
      content: '是否删除该活动',
      success: function (res) {
        if (res.confirm) {
          wh.Post('/api/mall.seckill/del', {
            sponsor_id: sponsor_id
          }, function (res) {
            wx.showToast({
              title: '删除成功',
            });
          })
          let arrs = that.data.lists;
          let arr = that.data.list;
          arr.splice(inx, 1)
          arrs.splice(inx, 1)
          that.setData({
            list: arr,
            lists: arrs
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  //删除(尚未开始)
  collect_dels: function (e) {
    let that = this,
      lists = this.data.lists,
      notice_id = e.currentTarget.dataset.id,
      inx = e.currentTarget.dataset.inx;
    wx.showModal({
      title: '',
      content: '是否删除该活动',
      success: function (res) {
        if (res.confirm) {
          wh.Post('/api/mall.seckill/del_notice', {
            notice_id: notice_id
          }, function (res) {
            wx.showToast({
              title: '删除成功',
            });
          })
          let arrs = that.data.lists;
          arrs.splice(inx, 1)
          that.setData({
            lists: arrs
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  // 倒计时(正在进行)
  countdown: function () {
    var that = this,
      list = this.data.list;
    for (let i = 0; i < list.length; i++) {
      var data = list[i].restTime;
      // 秒数  
      var second = data;
      // 天数位  
      var day = Math.floor(second / 3600 / 24);
      var dayStr = day.toString();
      if (dayStr.length == 1) dayStr = '0' + dayStr;
      // 小时位  
      var hr = Math.floor((second - day * 3600 * 24) / 3600);
      var hrStr = hr.toString();
      if (hrStr.length == 1) hrStr = '0' + hrStr;
      // 分钟位  
      var min = Math.floor((second - day * 3600 * 24 - hr * 3600) / 60);
      var minStr = min.toString();
      if (minStr.length == 1) minStr = '0' + minStr;
      // 秒位  
      var sec = second - day * 3600 * 24 - hr * 3600 - min * 60;
      var secStr = sec.toString();
      if (secStr.length == 1) secStr = '0' + secStr;
      list[i].restTime = list[i].restTime - 1;
      var lastList = this.data.list[i],
        lastTime = [];
      if (data <= 0) {
        lastTime = { "countDownDay": '00', "countDownHour": '00', "countDownMinute": '00', "countDownSecond": '00', 'notcut':'1'};
      } else {
        lastTime = { "countDownDay": dayStr, "countDownHour": hrStr, "countDownMinute": minStr, "countDownSecond": secStr, 'notcut': '0' };
      }
      lastList = Object.assign(lastList, lastTime)
    };
    this.setData({
      list: list
    })
  },
  // 倒计时(尚未开始)
  countdowns: function () {
    var that = this,
      lists = this.data.lists;
    for (let i = 0; i < lists.length; i++) {
      var data = lists[i].restTime;
      // 秒数  
      var second = data;
      // 天数位  
      var day = Math.floor(second / 3600 / 24);
      var dayStr = day.toString();
      if (dayStr.length == 1) dayStr = '0' + dayStr;
      // 小时位  
      var hr = Math.floor((second - day * 3600 * 24) / 3600);
      var hrStr = hr.toString();
      if (hrStr.length == 1) hrStr = '0' + hrStr;
      // 分钟位  
      var min = Math.floor((second - day * 3600 * 24 - hr * 3600) / 60);
      var minStr = min.toString();
      if (minStr.length == 1) minStr = '0' + minStr;
      // 秒位  
      var sec = second - day * 3600 * 24 - hr * 3600 - min * 60;
      var secStr = sec.toString();
      if (secStr.length == 1) secStr = '0' + secStr;
      lists[i].restTime = lists[i].restTime - 1;
      var lastList = this.data.lists[i],
        lastTime = [];
      if (data <= 0) {
        lastTime = { "countDownDay": '00', "countDownHour": '00', "countDownMinute": '00', "countDownSecond": '00', 'notcut':'1'};
      } else {
        lastTime = { "countDownDay": dayStr, "countDownHour": hrStr, "countDownMinute": minStr, "countDownSecond": secStr, 'notcut': '0' };
      }
      lastList = Object.assign(lastList, lastTime)
    };
    this.setData({
      lists: lists
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.ColletcGoods()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    clearInterval(this.countdown);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    clearInterval(this.data.down)
    this.setData({
      no_data: 0,
      page: 1,
      page_size: 5,
      onearth: false,
      list: [],
      lists: [],
    });
    this.ColletcGoods()
    this.CollectActivity()
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    clearInterval(this.data.down)
    let no_data = this.data.no_data,
      page = this.data.page,
      page_size = this.data.page_size;
    if (no_data == 0) {
      this.data.page = page + 1;
      this.ColletcGoods()
      this.CollectActivity()
    }
  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})